﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using FX.CC.Andon.Entity.BasicDataManage;
using FX.CC.Andon.Service.BasicDataManage;
using FX.CC.Andon.Utility.Common;
using FX.CC.Andon.Utility.Json;

namespace FX.CC.Andon.Business.BasicDataManage
{
    public class EquipmentBusiness
    {
        private readonly EquipmentService _dal = new EquipmentService();

        /// <summary>
        /// 获取设备绑定下拉列表
        /// </summary>
        /// <returns></returns>
        public string GetEquipmentListByCombobox(EquipmentQcEntity qcmodel)
        {
            return JsonHelper.Encode(_dal.GetEquipmentListByCombobox(qcmodel));
        }

        public string GetEquipmentList(EquipmentQcEntity qcmodel)
        {
            var total = _dal.GetEquipmentCount(qcmodel);
            var list = _dal.GetEquipmentList(qcmodel);
            return JsonHelper.Encode(total, list);
        }

        public bool AddEquipment(string data)
        {
            var model = new EquipmentEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            foreach (Hashtable row in rows)
            {
                model.EqID = Guid.NewGuid();
                model.LineID = new Guid(row["LineID"].ToString());
                model.EqCode = row["EqCode"] != null ? row["EqCode"].ToString() : "";
                model.EqName = row["EqName"] != null ? row["EqName"].ToString() : "";
                model.EqDesc = row["EqDesc"] != null ? row["EqDesc"].ToString() : "";
                model.RecStatus = true;
            }
            return _dal.AddEquipment(model);
        }

        public bool ModifyEquipment(string data)
        {
            var model = new EquipmentEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            foreach (Hashtable row in rows)
            {
                model.EqID = new Guid(row["EqID"].ToString());
                model.LineID = new Guid(row["LineID"].ToString());
                model.EqCode = row["EqCode"] != null ? row["EqCode"].ToString() : "";
                model.EqName = row["EqName"] != null ? row["EqName"].ToString() : "";
                model.EqDesc = row["EqDesc"] != null ? row["EqDesc"].ToString() : "";
            }
            return _dal.ModifyEquipment(model);
        }

        public string GetEquipmentInfo(string id)
        {
            return JsonHelper.Encode(_dal.GetEquipmentInfo(id));
        }

        public void ImportEq(string servicePath, string data)
        {
            var rows = (ArrayList)JsonHelper.Decode(data);
            var model = new EquipmentEntity();
            var excelPath = "";
            foreach (Hashtable row in rows)
            {
                excelPath = row["excUrl"].ToString();
                model.LineID = new Guid(row["LineID"].ToString());
            }
            var paht = servicePath + excelPath;
            var dt = ExcelHelper.Import(paht);
            var list = _dal.GetEquipmentListById(model.LineID.ToString());
            var excelList = GetList(dt, model.LineID);
            var updateList = new List<EquipmentEntity>();
            var addList = new List<EquipmentEntity>();
            if (list.Count > 0)
            {
                //去重复
                //var newlist =
                //    list.Union(excelList, new EquipmentComparer()).Except(list.Intersect(excelList, new EquipmentComparer()),
                //
                foreach (var i in excelList)
                {
                    var q = list.Where(T => T.EqCode.Equals(i.EqCode));
                    if (q.Count() > 0)
                    {
                        i.EqID = q.FirstOrDefault().EqID;
                        updateList.Add(i);
                    }
                    else
                    {
                        addList.Add(i);
                    }
                }
                if (addList.Count > 0)
                {
                    _dal.ImportEq(addList);
                }
                if (updateList.Count > 0)
                {
                    _dal.UpdateEq(updateList);
                }


            }
            else
            {
                //直接导入。
                _dal.ImportEq(excelList);
            }
        }
        private static List<EquipmentEntity> GetList(DataTable dt, Guid id)
        {
            var dr = dt.Select();

            var list = new List<EquipmentEntity>();
            foreach (var t in dr)
            {
                var model = new EquipmentEntity();
                model.EqID = Guid.NewGuid();
                model.LineID = id;
                var code = t[0].ToString().Trim().Replace("<空>", "");
                var name = t[1].ToString().Trim().Replace("<空>", "");
                if (string.IsNullOrEmpty(code) && string.IsNullOrEmpty(name))
                {
                    continue;
                }
                model.EqCode = code;
                model.EqName = name;
                model.EqDesc = t[2].ToString().Trim().Replace("<空>", "");
                model.RecStatus = true;
                list.Add(model);
            }
            return list;
        }
        public string GetCheckEqCode(string id)
        {
            return _dal.GetCheckEqCode(id);
        }

        public EquipmentEntity GetByCode(string eqCode)
        {
            return _dal.GetByCode(eqCode);
        }
    }
}
